"""
@Time ： 2025/8/6 15:12
@Auth ： zh
@File ：deal_billList.py
"""
import requests

import base_data
from deal_method import deal_method

#  用于项目提交前的 清单和工料机 数据批量处理  基于项目id
class DealBill:
    # 用于批量处理请求，只需要输入项目id即可
    @staticmethod
    def deal_bill_list(quotaId):
        # quotaId = input("请输入要处理的项目id:")
        # 获取到structureIds
        structureIds = deal_method.get_tree_id(quotaId)
        print("第一个结构树ID：", structureIds[0])
        costAccountId = deal_method.get_subject_id()
        print("科目id", costAccountId)
        # print(costAccountId)
        # 遍历 structureIds 获取billList
        for structureId in structureIds:
            # 提取到待处理的billList
            billList = deal_method.get_deal_billTree(quotaId, structureId)
            print("待处理清单为：", billList)
            # 直接发送请求处理
            bill_data = {
                "quotaId": quotaId,
                "structureId": structureId,
                "costAccountId": costAccountId,
                "billIds": billList
            }
            response = requests.post(
                url=base_data.deal_bill,
                json=bill_data,
                headers=base_data.headers,
                timeout=10
            )
            if response.status_code == 200:
                print(response.json())
                print("执行完成")

    @staticmethod
    def deal_material(quotaId):
        # 用于批量处理工料机数据
        # quotaId = input("请输入要处理的项目id:")
        # 获取到structureIds
        structureIds = deal_method.get_tree_id(quotaId)
        print("第一个结构树ID：", structureIds[0])
        # 获取到工料机分类id
        materialItemId = deal_method.get_CategoryMaterialItemTree(quotaId)
        print("工料机id为", materialItemId)
        for structureId in structureIds:
            materialList = deal_method.get_material_ids(quotaId, structureId)
            print("待处理工料机ID为：", materialList)
            material_data = {
                "quotaId": quotaId,
                "structureId": structureId,
                "materialItemId": materialItemId,
                "materialIds": materialList
            }
            response = requests.post(
                url=base_data.manual_Material,
                json=material_data,
                headers=base_data.headers,
                timeout=10
            )
            if response.status_code == 200:
                print(response.json())
                print("执行完成")

    # 登录方法  更新token  此处常用一个账号，所以写死，如果需要灵活可以调参
    @staticmethod
    def login():
        login_data = {
            "account": "test1",
            "password": "test123"
        }
        try:
            response = requests.post(
                url=base_data.login_url,
                json=login_data,
                headers={"Content-Type": "application/json"},
                timeout=10
            )
            if response.status_code == 200:
                response_data = response.json()
                # 假设返回的token在response_data['data']['token']中，根据实际响应调整
                token = response_data.get('data', {}).get('accessToken')
                print(token)
                if token:
                    # 更新base_data中的headers
                    base_data.headers["Authorization"] = f"bearer {token}"
                    print("登录成功，Token已更新")
                    return True
                else:
                    print("登录失败，未获取到token")
                    return False
            else:
                print(f"登录失败，状态码：{response.status_code}，错误信息：{response.text}")
                return False
        except requests.exceptions.RequestException as e:
            print(f"登录请求发生异常：{e}")
            return False


    # 方法中直接输入项目id 即可直接完成清单处理以及工料机数据的处理
    login()
    quotaId = input("请输入项目id:")
    deal_bill_list(quotaId)
    deal_material(quotaId)
